// 通用的JS工具封装
var _wm = require('util/wm.js')

var _user = {
    // 用户登录，传用户信息
    login: function(userInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/login.do'),
            method: 'POST',
            data: userInfo,
            success: resolve,
            error: reject
        })
    },

    // 检查用户名是否已存在
    checkUsername: function(username, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/check_valid.do'),
            method: 'POST',
            data: {
                type: 'username',
                str: username
            },
            success: resolve,
            error: reject
        })
    },

    // 用户注册
    register: function(userInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/register.do'),
            method: 'POST',
            data: userInfo,
            success: resolve,
            error: reject
        })
    },

    // 退出
    logout: function(resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/logout.do'),
            method: 'POST',
            success: resolve,
            error: reject
        })
    },

    // 检查登录状态
    checkLogin: function(resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/get_user_info.do'),
            method: 'POST',
            success: resolve,
            error: reject
        })
    },

    /** 
     * 找回密码-逻辑
    */
    // 获取用名密码提示问题
    getQuestion: function(username, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/forget_get_question.do'),
            method: 'POST',
            data: {
                username: username
            },
            success: resolve,
            error: reject
        })
    },
    // 检查密码提示问题答案
    checkAnswer: function(userInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/forget_get_answer.do'),
            method: 'POST',
            data: userInfo,
            success: resolve,
            error: reject
        })
    },
    // 重置密码
    resetPassword: function(userInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/forget_reset_password.do'),
            method: 'POST',
            data: userInfo,
            success: resolve,
            error: reject
        })
    },


    /** 
     * 个人中心
    */
    // 获取用户信息
    getUserInfo: function(resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/information.do'),
            method: 'POST',
            success: resolve,
            error: reject
        })
    },
    // 更改用户信息
    updateUserInfo: function(userInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/update_information.do'),
            method: 'POST',
            data: userInfo,
            success: resolve,
            error: reject
        })
    },
    // 登录状态下更新密码
    updatePassword: function(passwordInfo, resolve, reject){
        _wm.request({
            url: _wm.getServerUrl('/user/reset_password.do'),
            method: 'POST',
            data: passwordInfo,
            success: resolve,
            error: reject
        })
    }
}

module.exports = _user